package com.briskemen.client;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.briskemen.service.AddNumber;

public class Main2Activity extends AppCompatActivity {
    private static final String TAG = "Main2Activity";
    private AddNumber mAddNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.briskemen.service",
                "com.briskemen.service.AddNumberService"));
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                mAddNumber = AddNumber.Stub.asInterface(iBinder);
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        }, Context.BIND_AUTO_CREATE);
    }

    /**
     * 通过aidl进行加法运算
     * @param view
     */
    public void addNumber(View view){

        try {
            int total = mAddNumber.add(3, 4);
            Log.e(TAG,"total-------->"+total);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
